﻿using mars_server.csharp.cache;
using mars_server.csharp.dao;
using mars_server.csharp.dao.impl;
using mars_server.csharp.model;
using mars_server.csharp.socket;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using mars_server.csharp.CO;
using mars_server.csharp.Util;

namespace mars_server.csharp.AO.impl
{
    public class UserSocketAOImpl : UserSocketAO
    {
        //实现单例模式
        private UserSocketAOImpl() { }
        private static readonly UserSocketAOImpl instance = new UserSocketAOImpl();
        internal static UserSocketAOImpl Instance
        {
            get
            {
                return instance;
            }
        }


        RelationAO relationAO = RelationAOImpl.Instance;
        
        UserSocketManager userSocketManager = UserSocketManagerImpl.Instance;

        public void removeUserSocketByUserDO(UserDO userDO)
        {
            userSocketManager.deleteUserSocketByUserId(userDO.id);
            //发送消息体给相关的用户
        }

        /**
         * 搜寻在线好友
         * */
        public List<UserDO> queryOnlineUserDO()
        {
            List<UserDO> userDOs = new List<UserDO>();
            foreach(UserSocket userSocket in UserSocketCache.UserSocketList)
            {
                userDOs.Add(userSocket.userDO);
            }
            return userDOs;
        }
           
        public void deleteUserSocketByUserSocket(UserSocket userSocket)
        {
            if (userSocketManager.deleteUserSocketByUserSocketDO(userSocket))
            {
                Console.WriteLine("[info]:--【删除】套接字成功"+SocketUtil.getSocketInfo(userSocket));
                if(userSocket.userDO != null)
                {
                    List<UserDO> userDOs = relationAO.queryFriendDOsByUserId(userSocket.userDO.id);//好友do
                    foreach(UserDO userDO in userDOs)
                    {
                        this.sendMessageToUser(userDO,new MessageCO());
                    }
                }
            }
        }

        public void sendMessageToUser(UserDO userDO, MessageCO messageCO)
        {
            Console.WriteLine("[tip]【i am in】");
            UserSocket userSocket = userSocketManager.selectUserSocketByUserId(userDO.id);
            if(userSocket == null || userSocket.userDO == null)
            {
                Console.WriteLine("[warning]-----【用户未在线】--发送消息用户未在线-----");
                return;
            }
            this.sendMessageToSocket(userSocket, messageCO);
        }

        public void sendMessageToSocket(UserSocket userSocket, MessageCO messageCO)
        {
            try
            {
                Console.WriteLine("[info]:---【发送】---向 " + SocketUtil.getSocketInfo(userSocket) + "开始发送信息");
                userSocket.Bw.Write(JsonUtil.ObjToBytesUTF8(messageCO));
                Console.WriteLine("[info]:---【发送】---向 " + SocketUtil.getSocketInfo(userSocket) + "发送成功");
                Console.WriteLine("[info]:---【发送】---向 " + userSocket.userDO.id+ "发送成功");
            }
            catch (Exception)
            {
                this.deleteUserSocketByUserSocket(userSocket);
                Console.WriteLine("[error]:---【发送】---向 " + SocketUtil.getSocketInfo(userSocket) + "发送失败");
            }
         
        }

        public void deleteUserSocketByUserDO(UserDO userDO)
        {
            throw new NotImplementedException();
        }

        public bool isUserOnlie(long userId)//判断用户是否在线
        {
            foreach(UserSocket userSocket in UserSocketCache.UserSocketList)
            {
                if (userSocket != null && userSocket.userDO != null)
                {
                    if (userId == userSocket.userDO.id)
                    {
                        return true;
                    }
                }
            }
            return false;
        }
    }
}
